import React, { PureComponent } from 'react'
import PropTypes from 'prop-types'
import store from '../store'
import { subAction } from '../store/actionCreators'

class test extends PureComponent {
	constructor(props) {
		super(props)
		this.state = {
			counter: store.getState().counter,
		}
	}
	componentDidMount() {
		this.unsubscribue = store.subscribe(() => {
			this.setState({
				counter: store.getState().counter,
			})
		})
	}

	componentWillUnmount() {
		this.unsubscribue()
	}
	render() {
		return (
			<div>
				<h1>test</h1>
				<h2>当前计数: {this.state.counter}</h2>
				<button onClick={(e) => this.increment()}>-1</button>
				<button onClick={(e) => this.addNumber(5)}>-5</button>
			</div>
		)
	}

	increment() {
		store.dispatch(subAction(1))
	}

	addNumber(num) {
		store.dispatch(subAction(num))
	}
}

export default test
